/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * STAFF_DELETE.java
 *
 * Created on Sep 27, 2010, 12:02:35 AM
 */

package COMIC_BOOK_STORE;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Alexander
 */
public class STAFF_DELETE extends javax.swing.JFrame {

    Connection con;
    Statement stat;
    ResultSet rset;
    /** Creates new form STAFF_DELETE */
    public STAFF_DELETE() throws SQLException {
        super("DELETE STAFF");
        initComponents();
        this.setLocationRelativeTo(null);
        connect();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        canvas1 = new java.awt.Canvas();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        mainMenuButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        displaymobilePhoneNumber = new javax.swing.JLabel();
        mobilePhoneNumberLabel = new javax.swing.JLabel();
        displayHousePhoneNumber = new javax.swing.JLabel();
        housephoneNumberLabel = new javax.swing.JLabel();
        displaystaffCode = new javax.swing.JLabel();
        staffCodeLabel = new javax.swing.JLabel();
        displayIcNumber = new javax.swing.JLabel();
        icNumberLabel = new javax.swing.JLabel();
        displayLastName = new javax.swing.JLabel();
        lastNameLabel = new javax.swing.JLabel();
        displayFirstName = new javax.swing.JLabel();
        firstNameLabel = new javax.swing.JLabel();
        staffDetailsLabel = new javax.swing.JLabel();
        lookupByLabel = new javax.swing.JLabel();
        choiceComboBox = new javax.swing.JComboBox();
        choiceTextField = new javax.swing.JTextField();
        findButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();

        jLabel15.setText("WARNING!");

        jLabel16.setText("This will permanently delete the selected employee.");

        jLabel17.setText("Are you sure?");

        jButton4.setText("PROCEED");

        jButton5.setText("CANCEL");

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialog1Layout.createSequentialGroup()
                .addContainerGap(420, Short.MAX_VALUE)
                .addComponent(jButton4)
                .addGap(26, 26, 26)
                .addComponent(jButton5)
                .addGap(45, 45, 45))
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel17)
                    .addComponent(jLabel15)
                    .addComponent(jLabel16))
                .addContainerGap(288, Short.MAX_VALUE))
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addComponent(jLabel15)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel16)))
                .addGap(18, 18, 18)
                .addComponent(jLabel17)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addGap(23, 23, 23))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        mainMenuButton.setText("MAIN MENU");
        mainMenuButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mainMenuButtonActionPerformed(evt);
            }
        });
        mainMenuButton.setBounds(730, 430, 120, 30);
        jLayeredPane1.add(mainMenuButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        backButton.setText("BACK");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        backButton.setBounds(630, 430, 90, 30);
        jLayeredPane1.add(backButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        deleteButton.setText("DELETE");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });
        deleteButton.setBounds(530, 430, 90, 30);
        jLayeredPane1.add(deleteButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displaymobilePhoneNumber.setText("x");
        displaymobilePhoneNumber.setBounds(310, 330, 210, 30);
        jLayeredPane1.add(displaymobilePhoneNumber, javax.swing.JLayeredPane.DEFAULT_LAYER);

        mobilePhoneNumberLabel.setText("Mobile Phone No.");
        mobilePhoneNumberLabel.setBounds(110, 330, 140, 30);
        jLayeredPane1.add(mobilePhoneNumberLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayHousePhoneNumber.setText("x");
        displayHousePhoneNumber.setBounds(310, 370, 170, 30);
        jLayeredPane1.add(displayHousePhoneNumber, javax.swing.JLayeredPane.DEFAULT_LAYER);

        housephoneNumberLabel.setText("House Telephone No.");
        housephoneNumberLabel.setBounds(110, 370, 160, 30);
        jLayeredPane1.add(housephoneNumberLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displaystaffCode.setText("x");
        displaystaffCode.setBounds(310, 250, 210, 30);
        jLayeredPane1.add(displaystaffCode, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffCodeLabel.setText("Staff Code");
        staffCodeLabel.setBounds(110, 260, 120, -1);
        jLayeredPane1.add(staffCodeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayIcNumber.setText("x");
        displayIcNumber.setBounds(310, 290, 200, 30);
        jLayeredPane1.add(displayIcNumber, javax.swing.JLayeredPane.DEFAULT_LAYER);

        icNumberLabel.setText("IC Number");
        icNumberLabel.setBounds(110, 300, 70, -1);
        jLayeredPane1.add(icNumberLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayLastName.setText("x");
        displayLastName.setBounds(310, 210, 200, 30);
        jLayeredPane1.add(displayLastName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lastNameLabel.setText("Last Name");
        lastNameLabel.setBounds(110, 220, 110, 20);
        jLayeredPane1.add(lastNameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayFirstName.setText("x");
        displayFirstName.setBounds(310, 170, 190, 30);
        jLayeredPane1.add(displayFirstName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        firstNameLabel.setText("First Name");
        firstNameLabel.setBounds(110, 180, 130, 30);
        jLayeredPane1.add(firstNameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffDetailsLabel.setText("Staff Details");
        staffDetailsLabel.setBounds(100, 140, 90, 20);
        jLayeredPane1.add(staffDetailsLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lookupByLabel.setText("Lookup by");
        lookupByLabel.setBounds(100, 30, 140, 30);
        jLayeredPane1.add(lookupByLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        choiceComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "Staff Code", "IC Number" }));
        choiceComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                choiceComboBoxActionPerformed(evt);
            }
        });
        choiceComboBox.setBounds(280, 30, 120, 30);
        jLayeredPane1.add(choiceComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);
        choiceTextField.setBounds(440, 30, 170, 30);
        jLayeredPane1.add(choiceTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        findButton.setText("FIND");
        findButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findButtonActionPerformed(evt);
            }
        });
        findButton.setBounds(660, 30, 90, 30);
        jLayeredPane1.add(findButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(0, 100, 900, 20);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGE_REPOSITORY/Background3 copy.jpg"))); // NOI18N
        jLabel1.setText("jLabel1");
        jLabel1.setBounds(0, 0, 900, 500);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

        String query = null;
        String choice = choiceTextField.getText();
        String combo_select = (String) choiceComboBox.getSelectedItem();

        if(choice.equals(""))
        {
            JOptionPane.showMessageDialog(null, "Please select a staff to delete!!");
        }
        else
        {

            if(combo_select.equals("Staff Code")) {
            query = "DELETE FROM STAFF WHERE STAFF_CODE ="+ choice +"";
            }

            if(combo_select.equals("IC Number")) {
            query = "DELETE FROM STAFF WHERE STAFF_ICNO ="+ choice +"";
            }

            String message = "Are you sure to delete?";
            String title = "DELETE STAFF";

            int reply = JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_OPTION);
            if(reply == JOptionPane.YES_OPTION) {
            //PreparedStatement pstat;

                try {

                stat = con.createStatement();
                stat.executeUpdate(query);

                JOptionPane.showMessageDialog(null, "Delete Successful");
                } catch (SQLException ex) {
                Logger.getLogger(STAFF_ADD.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Delete Failed!");
                }
            }

            clear();
        }
        // TODO add your handling code here:
}                                            

    private void choiceComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
}                                              

    private void findButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        String query = null;
        String choice = choiceTextField.getText();
        String combo_select = (String) choiceComboBox.getSelectedItem();

        if(combo_select.equals("Select")) {
            JOptionPane.showMessageDialog(null, "Please select lookup by first!");

        }
    else{

        if(combo_select.equals("Staff Code")) {
            query = "SELECT STAFF_FNAME,STAFF_LNAME,STAFF_ICNO,STAFF_CODE,"+
                    "STAFF_HOUSEPHONE,STAFF_MOBILEPHONE FROM STAFF WHERE STAFF_CODE ="+ choice +"";
        }

        if(combo_select.equals("IC Number")) {
            query = "SELECT STAFF_FNAME,STAFF_LNAME,STAFF_ICNO,STAFF_CODE,"+
                    "STAFF_HOUSEPHONE,STAFF_MOBILEPHONE FROM STAFF WHERE STAFF_ICNO ="+ choice +"";
        }
        try {
            stat = con.createStatement();
            rset = stat.executeQuery(query);

            if(rset.next()) {
                displayFirstName.setText(rset.getString("STAFF_FNAME"));
                displayLastName.setText(rset.getString("STAFF_LNAME"));
                displayIcNumber.setText(rset.getString("STAFF_ICNO"));
                displaystaffCode.setText(rset.getString("STAFF_CODE"));
                displayHousePhoneNumber.setText(rset.getString("STAFF_HOUSEPHONE"));
                displaymobilePhoneNumber.setText(rset.getString("STAFF_MOBILEPHONE"));

            }

        } catch (SQLException ex) {
            Logger.getLogger(STAFF_DELETE.class.getName()).log(Level.SEVERE, null, ex);
        }
       }
        // TODO add your handling code here:
}                                          

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try {
            new STAFF_RECORD().setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(STAFF_DELETE.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setVisible(false);// TODO add your handling code here:
    }                                          

    private void mainMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    new MAIN_MENU().setVisible(true);
    this.setVisible(false);// TODO add your handling code here:
    }                                              

    private void clear()
    {
        displayFirstName.setText("");
                displayLastName.setText("");
                displayIcNumber.setText("");
                displaystaffCode.setText("");
                displayHousePhoneNumber.setText("");
                displaymobilePhoneNumber.setText("");
    }
    private void connect() throws SQLException
    {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        System.out.println("Driver Loaded");
        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","javaproject","javaproject");
        System.out.println("Database Connected");

    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new STAFF_DELETE().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(STAFF_DELETE.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton backButton;
    private java.awt.Canvas canvas1;
    private javax.swing.JComboBox choiceComboBox;
    private javax.swing.JTextField choiceTextField;
    private javax.swing.JButton deleteButton;
    private javax.swing.JLabel displayFirstName;
    private javax.swing.JLabel displayHousePhoneNumber;
    private javax.swing.JLabel displayIcNumber;
    private javax.swing.JLabel displayLastName;
    private javax.swing.JLabel displaymobilePhoneNumber;
    private javax.swing.JLabel displaystaffCode;
    private javax.swing.JButton findButton;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JLabel housephoneNumberLabel;
    private javax.swing.JLabel icNumberLabel;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JLabel lookupByLabel;
    private javax.swing.JButton mainMenuButton;
    private javax.swing.JLabel mobilePhoneNumberLabel;
    private javax.swing.JLabel staffCodeLabel;
    private javax.swing.JLabel staffDetailsLabel;
    // End of variables declaration                   

}
